package com.frame.demo.app;

import android.app.Activity;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.frame.demo.R;
import com.frame.demo.rest.ExceptionDispose;
import com.frame.demo.rest.RestError;

/**
 * @author yuefeng
 * @version 3.4.3
 * @desc 和fragment的一些共用处理
 * @date 16/3/15
 */
public class ActivityHelperImpl implements ActivityHelper {
    private Activity mActivity;
    private LayoutInflater mLayoutInflater;
    private LinearLayout mContainerLayout;
    private ExceptionDispose mExceptionDispose;
    private View mContentView;


    public ActivityHelperImpl(LinearLayout pContainerLayout, Activity pActivity, View pContentView) {
        mContainerLayout = pContainerLayout;
        mLayoutInflater = pActivity.getLayoutInflater();
        mActivity = pActivity;
        this.mContentView = pContentView;
    }


    @Override
    public void showErrorView(@Nullable String pErrorMes, @DrawableRes int pErrorIconRes) {
        mContainerLayout.removeAllViews();
        mLayoutInflater.inflate(R.layout.common_network_error_page, mContainerLayout, true);
        ((ImageView) findViewById(R.id.iv_no_network)).setImageResource(pErrorIconRes);
        if (!TextUtils.isEmpty(pErrorMes)) {
            ((TextView) findViewById(R.id.tv_no_network)).setText(pErrorMes);
        }

    }

    private View findViewById(int idRes) {
        return mActivity.getWindow().findViewById(idRes);
    }

    @Override
    public void showErrorView(RestError pRestError) {
        if (null == pRestError) return;
        mContainerLayout.removeAllViews();
        mContainerLayout.addView(getErrorViewLayoutRes(pRestError));

    }

    @Override
    public void showInPageProgressView() {
        if (mContainerLayout.getChildCount() > 0) {
            mContainerLayout.removeAllViews();
        }
        mLayoutInflater.inflate(R.layout.loading_view, mContainerLayout);
    }

    @Override
    public void commonExceptionDispose(RestError pRestError) {
        if (null == pRestError) return;
        if (null == mExceptionDispose) {
            this.mExceptionDispose = new ExceptionDispose(mActivity);
        }
        this.mExceptionDispose.disponse(pRestError);

    }

    @Override
    public void showContent() {
        if (mContainerLayout.getChildCount() > 0) {
            mContainerLayout.removeAllViews();
        }
        mContainerLayout.addView(mContentView);

    }

    @Override
    public void showErrorView(ViewGroup pViewGroupParent, String pErrorMes, @IdRes int pErrorIconRes) {
        if (null == pViewGroupParent) return;
        pViewGroupParent.removeAllViews();
        mLayoutInflater.inflate(R.layout.common_network_error_page, pViewGroupParent, true);
    }

    private View getErrorViewLayoutRes(RestError pRestError) {
        switch (pRestError.getErrorType()) {
            case ExceptionDispose.EXCEPTION_DIRTY_DATA:
            case ExceptionDispose.SUCCESS:
            case ExceptionDispose.EXCEPTION_BUSINESS:

                return generateDirtyDataExceptionView(pRestError);
            default:
                return generateNetworkExceptionVieW(pRestError);
        }
    }

    private View generateNetworkExceptionVieW(RestError pRestError) {
        View _view = mLayoutInflater.inflate(R.layout.common_network_error_page, mContainerLayout, false);
        ((TextView) _view.findViewById(R.id.tv_no_network)).setText(pRestError.getMsg());
        return _view;
    }

    private View generateDirtyDataExceptionView(RestError pRestError) {
        View _view = mLayoutInflater.inflate(R.layout.exception_dirty_data_layout, mContainerLayout, false);
        ((TextView) _view.findViewById(R.id.tv_message)).setText(pRestError.getMsg());
        return _view;
    }


}
